package waal.pc.frontend;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import waal.pc.backend.Log;


public class LogTab {
	
	private JLabel debugLevelValue;
	private JPanel topPane = new JPanel();
	
	
	public LogTab() {
		this.topPane.setLayout( new BoxLayout(this.topPane, BoxLayout.Y_AXIS) );
		this.topPane.add(this.createLogPane());
		this.topPane.add(this.createButtonPane());
		
		Log.addToLog("Starte Log. Debug Level ist: " + Log.debuglevel);
		
	}
	
	private JScrollPane createLogPane() {
		JTextPane p = new JTextPane();
		p.setDocument(Log.getLogDoc());
		p.setEditable(false);
		JScrollPane sp = new JScrollPane(p);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(250,250));
		return sp;
	}
	
	private JPanel createButtonPane() {
		JPanel p = new JPanel();
		p.add(new JLabel("Debug level: "));
		this.debugLevelValue = new JLabel(Integer.toString(Log.debuglevel));
		p.add(this.debugLevelValue);
		p.add(this.createIncDeButton());
		p.add(this.createDecDeButton());
		return p;
	}
	
	private JButton createIncDeButton() {
		JButton button = new JButton( "Up" );
		ActionListener al = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(Log.debuglevel < 9 ) {
					Log.debuglevel++;
					debugLevelValue.setText(Integer.toString(Log.debuglevel));
				}
			}
		};
		button.addActionListener(al);
		return button;
	}
	
	private JButton createDecDeButton() {
		JButton button = new JButton( "Down" );
		ActionListener al = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(Log.debuglevel > 0 ) {
					Log.debuglevel--;
					debugLevelValue.setText(Integer.toString(Log.debuglevel));
				}
			}
		};
		button.addActionListener(al);
		return button;
	}
	
	
	
	public JPanel getTabPane() {
		return this.topPane;
	}
	
	
}
